package com.example.yichuangyun.mvvm.ui.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemRiskChildBinding;
import com.example.yichuangyun.databinding.ItemRiskInfoBinding;
import com.example.yichuangyun.model.company.IntellectualPropertyDataModel;
import com.example.yichuangyun.model.company.ManageInfoDataModel;
import com.example.yichuangyun.utils.CommonUtil;

import java.util.List;

/**
 * 产权证书
 */
public class IntellectualPropertyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<IntellectualPropertyDataModel> data;

    public IntellectualPropertyAdapter(Context context, List<IntellectualPropertyDataModel> data) {
        this.context = context;
        this.data = data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemRiskInfoBinding binding = ItemRiskInfoBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            MyViewHolder myViewHolder = (MyViewHolder) holder;
            IntellectualPropertyDataModel model = data.get(position);
            switch (position) {
                case 0:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getTtrademarkInfos().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getTtrademarkInfos(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getTtrademarkInfos().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getTtrademarkInfos().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getTtrademarkInfos().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 1:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getCopyrightsRelationsList().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getCopyrightsRelationsList(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getCopyrightsRelationsList().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getCopyrightsRelationsList().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getCopyrightsRelationsList().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 2:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getCopyrightsRelations().size()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getCopyrightsRelations(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getCopyrightsRelations().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getCopyrightsRelations().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getCopyrightsRelations().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
                case 3:
                    myViewHolder.binding.tvCount.setText(context.getString(R.string.tab_count, model.getzLCount()));
                    myViewHolder.binding.recyclerView.setAdapter(new MyAdapter(model.getPatentsRelationsNews(), position));
                    myViewHolder.binding.ivArrow.setVisibility(model.getPatentsRelationsNews().size() > 0 ? View.VISIBLE : View.GONE);
                    myViewHolder.binding.tvNoData.setVisibility(model.getPatentsRelationsNews().size() > 0 ? View.GONE : View.VISIBLE);
                    myViewHolder.binding.tvCount.setVisibility(model.getPatentsRelationsNews().size() > 0 ? View.VISIBLE : View.GONE);
                    break;
            }
            myViewHolder.binding.recyclerView.setVisibility(model.isOpen() ? View.VISIBLE : View.GONE);
            myViewHolder.binding.ivArrow.setImageResource(model.isOpen() ? R.mipmap.icon_up : R.mipmap.icon_down);
            myViewHolder.binding.tvTitle.setText(model.getTitle());

            myViewHolder.binding.llTitle.setEnabled(data.size() > 0);
            myViewHolder.binding.llTitle.setOnClickListener(view -> {
                model.setOpen(!model.isOpen());
                notifyItemChanged(holder.getAbsoluteAdapterPosition());
            });
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemRiskInfoBinding binding;

        public MyViewHolder(ItemRiskInfoBinding itemRiskInfoBinding) {
            super(itemRiskInfoBinding.getRoot());
            binding = itemRiskInfoBinding;
        }
    }

    class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private final List<IntellectualPropertyDataModel.IntellectualPropertyModel> mData;
        private final int parentPosition;

        public MyAdapter(List<IntellectualPropertyDataModel.IntellectualPropertyModel> mData, int parentPosition) {
            this.mData = mData;
            this.parentPosition = parentPosition;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ItemRiskChildBinding binding = ItemRiskChildBinding.inflate(LayoutInflater.from(context), parent, false);
            return new ManageViewHolder(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof ManageViewHolder) {
                ManageViewHolder myViewHolder = (ManageViewHolder) holder;
                IntellectualPropertyDataModel.IntellectualPropertyModel model = mData.get(position);
                switch (parentPosition) {
                    case 0: // 商标信息
                        myViewHolder.binding.llLogo.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText6.setVisibility(View.GONE);
                        Glide.with(context).load(model.getImageUrl()).into(myViewHolder.binding.ivLogo);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.trademark_name, CommonUtil.setText3(model.getTrademarkName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.resister_no, CommonUtil.setText3(model.getRegNumber())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.apply_date, CommonUtil.setText3(model.getApplyDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.logo_type, CommonUtil.setText3(model.getTypeNum())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.trademark_status, CommonUtil.setText3(model.getTrademarkStatus())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 1: // 作品著作权
                        myViewHolder.binding.llLogo.setVisibility(View.GONE);
                        myViewHolder.binding.tvText6.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.works_name, CommonUtil.setText3(model.getName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.resister_no, CommonUtil.setText3(model.getNumber())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.works_type, CommonUtil.setText3(model.getType())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.works_finish_date, CommonUtil.setText3(model.getSuccessDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.register_date, CommonUtil.setText3(model.getApprovalDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText6.setText(Html.fromHtml(context.getString(R.string.first_publish_date, CommonUtil.setText3(model.getFirstDate())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 2: // 软件著作权
                        myViewHolder.binding.llLogo.setVisibility(View.GONE);
                        myViewHolder.binding.tvText6.setVisibility(View.VISIBLE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.software_name, CommonUtil.setText3(model.getName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.software_reg_no, CommonUtil.setText3(model.getNumber())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.software_version_no, CommonUtil.setText3(model.getVersion())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.software_classify_no, CommonUtil.setText3(model.getTypeNum())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.register_ratify_date, CommonUtil.setText3(model.getApprovalDate())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText6.setText(Html.fromHtml(context.getString(R.string.software_abbreviation, CommonUtil.setText3(model.getShortName())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                    case 3: // 专利
                        myViewHolder.binding.llLogo.setVisibility(View.GONE);
                        myViewHolder.binding.tvText6.setVisibility(View.GONE);
                        myViewHolder.binding.tvText1.setText(Html.fromHtml(context.getString(R.string.patent_name, CommonUtil.setText3(model.getPatentName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText2.setText(Html.fromHtml(context.getString(R.string.patent_type, CommonUtil.setText3(model.getTypeName())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText3.setText(Html.fromHtml(context.getString(R.string.patent_auth_status, CommonUtil.setText3(model.getAuthorizeTag())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText4.setText(Html.fromHtml(context.getString(R.string.patent_apply_no, CommonUtil.setText3(model.getRequestNumStandard())), Html.FROM_HTML_MODE_LEGACY));
                        myViewHolder.binding.tvText5.setText(Html.fromHtml(context.getString(R.string.patent_apply_date, CommonUtil.setText3(model.getRequestDate())), Html.FROM_HTML_MODE_LEGACY));
                        break;
                }
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }
    }

    static class ManageViewHolder extends RecyclerView.ViewHolder {

        ItemRiskChildBinding binding;

        public ManageViewHolder(ItemRiskChildBinding itemRiskChildBinding) {
            super(itemRiskChildBinding.getRoot());
            binding = itemRiskChildBinding;
        }
    }
}
